package cn.tedu.overhaul.management.dto.persist.repository.impl;

import cn.tedu.overhaul.management.dto.persist.mapper.OverhaulMapper;
import cn.tedu.overhaul.management.dto.persist.mapper.OverhaulRentMapper;
import cn.tedu.overhaul.management.dto.persist.repository.IOverhaulRepository;
import cn.tedu.overhaul.management.pojo.entity.Overhaul;
import cn.tedu.overhaul.management.pojo.vo.*;
import cn.tedu.rtms.commons.ex.ServiceException;
import cn.tedu.rtms.commons.pojo.vo.PageData;
import cn.tedu.rtms.commons.util.PageInfoToPageDataConverter;
import cn.tedu.rtms.commons.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Repository
public class OverhaulRepositoryImpl implements IOverhaulRepository {
    @Autowired
    OverhaulMapper overhaulMapper;

    @Autowired
    OverhaulRentMapper overhaulRentMapper;

    @Override
    public PageData<MaintainVO> selectMaintain(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询检修记录表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<MaintainUserIdVO> list=overhaulMapper.selectMaintain();
        log.debug("list：{}", list);

        PageInfo<MaintainUserIdVO> pageInfoOriginal = new PageInfo<>(list);
        long total = pageInfoOriginal.getTotal();
        int pages = pageInfoOriginal.getPages();

        List<MaintainVO> list2=new ArrayList<>();

        for (MaintainUserIdVO maintainUserIdVO : list) {
            MaintainVO maintainVO = new MaintainVO();
            maintainVO.setId(maintainUserIdVO.getId());
            String name=overhaulRentMapper.selectById(maintainUserIdVO.getUserId());
            maintainVO.setName(name);
            maintainVO.setTime(maintainUserIdVO.getTime());
            maintainVO.setToolName(maintainUserIdVO.getToolName());
            maintainVO.setToolNumber(maintainUserIdVO.getToolNumber());
            maintainVO.setDuration(maintainUserIdVO.getDuration());
            maintainVO.setDurationStart(maintainUserIdVO.getDurationStart());
            maintainVO.setDurationEnd(maintainUserIdVO.getDurationEnd());
            maintainVO.setTarget(maintainUserIdVO.getTarget());
            maintainVO.setContent(maintainUserIdVO.getContent());
            list2.add(maintainVO);
        }
        log.debug("list2：{}", list2);
        PageInfo<MaintainVO> pageInfo = new PageInfo<>(list2);
        pageInfo.setTotal(total); // 设置正确的总记录数
        pageInfo.setPages(pages); // 设置正确的总页数
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        log.debug("pageInfo={}",pageInfo);
        PageData<MaintainVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public MaintainStandardNameVO getStandardById(Long id) {
        log.debug("开始执行【根据ID修改面板查询检修记录】的数据访问，参数：{}", id);
        MaintainStandardVO maintainStandardVO=overhaulMapper.getStandardById(id);
        if(maintainStandardVO==null){
            //是:抛出异常
            String message ="根据ID修改面板查询检修记录失败，根据ID修改面板查询检修记录不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        if(maintainStandardVO.getModificationTime()==null){
            maintainStandardVO.setModificationTime("未修改");
        }
        if(maintainStandardVO.getModificationFrequency()==null){
            maintainStandardVO.setModificationFrequency(0);
        }
        MaintainStandardNameVO maintainStandardNameVO=new MaintainStandardNameVO();
        maintainStandardNameVO.setId(maintainStandardVO.getId());
        String name=overhaulRentMapper.selectById(maintainStandardVO.getUserId());
        maintainStandardNameVO.setName(name);
        maintainStandardNameVO.setToolName(maintainStandardVO.getToolName());
        maintainStandardNameVO.setToolNumber(maintainStandardVO.getToolNumber());
        maintainStandardNameVO.setDuration(maintainStandardVO.getDuration());
        maintainStandardNameVO.setDurationStart(maintainStandardVO.getDurationStart());
        maintainStandardNameVO.setDurationEnd(maintainStandardVO.getDurationEnd());
        maintainStandardNameVO.setTarget(maintainStandardVO.getTarget());
        maintainStandardNameVO.setContent(maintainStandardVO.getContent());
        maintainStandardNameVO.setModificationTime(maintainStandardVO.getModificationTime());
        maintainStandardNameVO.setModificationFrequency(maintainStandardVO.getModificationFrequency());
        return maintainStandardNameVO;
    }

    @Override
    public int update(OverhaulVO overhaulVO) {
        log.debug("开始执行【更新检修记录】的数据访问，参数：{}", overhaulVO);
        return overhaulMapper.updateById(overhaulVO);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除相册】的数据访问，参数：{}", id);
        return overhaulMapper.deleteById(id);
    }
}
